package gameobjects;

public class Beer extends Element {
 
	private boolean madeByMagicWallTransformation = false;
	
        /**
         * Constructor.
         */
        public Beer(){
            madeByMagicWallTransformation = false;
        }
        
        /**
         * Move the beer.
         */
	public void move(Hero hero) {
            int beerPositionX = this.getBox().getPositionX();
            int beerPositionY = this.getBox().getPositionY();
            int targetPositionY = beerPositionY-1;
            int offset = 0;
            //search until what position the beer can move, this is clean terrain
            while((hero.getCurrentBoard().getBoxes().getBox(beerPositionX, targetPositionY).getElement().getId()==Element.CleanTerrainId)){
                offset++;
                targetPositionY = targetPositionY-1;
            }
            targetPositionY = beerPositionY-offset;
            //if under the last clean terrain is the hero, then this die
            if((hero.getBox().getPositionX()==beerPositionX)&&(hero.getBox().getPositionY()==targetPositionY-1)){
                hero.getCurrentBoard().getGame().subtractALife();
            }
            //if not and offset is at least 1, swap positions
            else{
                //introduce the beer box into the updated boxes
                hero.getCurrentBoard().getUpdatedBoxes().add(hero.getCurrentBoard().getBoxes().getBox(beerPositionX, beerPositionY));
                if(offset>0){
                    //introduce the target box into the updated boxes
                    hero.getCurrentBoard().getUpdatedBoxes().add(hero.getCurrentBoard().getBoxes().getBox(beerPositionX, targetPositionY));
                    hero.getCurrentBoard().getBoxes().swapBoxes(beerPositionX, beerPositionY, beerPositionX, targetPositionY);
                }
            }
            
	}
	
        /**
         * Indicates if the beer is made by wall transformation
         * @return
         */
	public boolean isMadeByMagicWallTransformation() {
		
            return madeByMagicWallTransformation;
	}
	 
}
 
